<?php

// mbilling/BillingBundle/Entity/users.php
namespace mbilling\BillingBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 */
class users implements UserInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=30, unique="TRUE")
     * @assert\NotBlank
     * @assert\Email
     * @assert\MaxLength(30)
     */
    protected $login;
    
    /**
     * @ORM\Column(name="salt", type="string", length=40)
     */
    private $salt;

    /**
     * @ORM\Column(name="password", type="string", length=40)
     */
    private $password;
    
    /**
     * @ORM\Column(type="integer")
     */
    protected $status_id;

    protected $password_confirm;
    
    protected $open_password;
    
    protected $roles=array('ROLE_USER');
    
    /**
     * @ORM\Column(name="is_active", type="boolean")
     */
    protected $isActive;

    protected $factory;
    
    public function __construct($factory)
    {
        $this->isActive = true;
        $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
        $this->factory = $factory;
    }
    
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getId()
    {
        return $this->id;
    }

    public function setLogin($login)
    {
        $this->login = $login;
    }
    public function getLogin()
    {
        return $this->login;
    }

    public function setPassword($password)
    {
        $this->open_password = $password;
        $encoder = $this->factory->getEncoder($this);
        $enc_password = $encoder->encodePassword($password, $this->getSalt());
        $this->password = $enc_password;
    }
    public function getPassword()
    {
        return $this->password;
    }
    public function getOpenPassword()
    {
        return $this->open_password;
    }
    
    public function setStatusId($status_id)
    {
        $this->status_id = $status_id;
    }
    public function getStatusId()
    {
        return $this->status_id;
    }

    public function setPasswordConfirm($password_confirm)
    {
        $this->password_confirm = $password_confirm;
    }
    public function getPasswordConfirm()
    {
        return $this->password_confirm;
    }
    
    public function getAutologinHash()
    {
        return sha1( md5($this->getId()).$this->getSalt() );
    }
    
    public function isAdmin()
    {
        if ($this->getStatusId() == '1')
            return true;
        else
            return false;
    }
    public function initDefaultEntity()
    {
        $this->setLogin('login');
        $this->setPassword('password');
        $this->setStatusId('1');
        return $this;
    }
    
    public function getRoles()
    {
        return $this->roles;
    }
    public function getSalt()
    {
        return $this->salt;
    }
    public function getUsername()
    {
        return $this->login;
    }
    public function eraseCredentials()
    {

    }
    public function equals(UserInterface $account)
    {
        if (!$account instanceof $this) {
            return false;
        }

        if ($this->password !== $account->getPassword()) {
            return false;
        }

        if ($this->getSalt() !== $account->getSalt()) {
            return false;
        }

        if ($this->login !== $account->getUsername()) {
            return false;
        }
        return true;
    }
}

?>
